using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.DataControls
{
    [ToolboxData("<{0}:PageHirechicalDataSource runat=server></{0}:PageHirechicalDataSource>")]
    public class PageHirechicalDataSource : HierarchicalDataSourceControl
    {
        private PageHirechicalDataSourceView _view;
        #region IHierarchicalDataSource Members

        protected override HierarchicalDataSourceView  GetHierarchicalView(string viewPath)
        {
            if (_view == null)
            {
                _view = new PageHirechicalDataSourceView();
                _view.GetData += new EventHandler<PageHirechicalSelectEventArgs>(_view_GetData);
            }
            return _view;
        }

        void _view_GetData(object sender, PageHirechicalSelectEventArgs e)
        {
            if (GetData != null)
            {
                GetData(sender, e);
            }
        }

        public event EventHandler<PageHirechicalSelectEventArgs> GetData;

        #endregion

        #region IHierarchicalDataSource Members

        public event EventHandler DataSourceChanged;
        public void OnDataSourceChanged()
        {
            if (DataSourceChanged != null)
            {
                DataSourceChanged(this, EventArgs.Empty);
            }
        }


        #endregion
    }

    public class PageHirechicalDataSourceView : HierarchicalDataSourceView
    {
        public override IHierarchicalEnumerable Select()
        {
            if (GetData != null)
            {
                PageHirechicalSelectEventArgs e = new PageHirechicalSelectEventArgs();
                GetData(this, e);
                if (e.GetData() != null)
                {
                    return new PageHirechicalEnumerable(e.GetData());
                }
            }
            return null;
        }

        public event EventHandler<PageHirechicalSelectEventArgs> GetData;
    }

    public class PageHirechicalSelectEventArgs : System.EventArgs
    {
        public void SetData(Acms.Bll.Page page)
        {
            if (_pages == null)
            {
                _pages = new Acms.Bll.PageCollection();
                _pages.Add(page);
            }
            else
            {
                throw new ApplicationException("Data Already set");
            }

        }

        public void SetData(Acms.Bll.PageCollection pages)
        {
            _pages = pages;
        }

        private Acms.Bll.PageCollection _pages;
        internal Acms.Bll.PageCollection GetData()
        {
            return _pages;
        }

    }

    public class PageHirechicalEnumerable : IHierarchicalEnumerable
    {
        public PageHirechicalEnumerable(Acms.Bll.PageCollection pages)
        {
            _pages = pages;
        }

        private Acms.Bll.PageCollection _pages;

        #region IHierarchicalEnumerable Members
        public IHierarchyData GetHierarchyData(object enumeratedItem)
        {
            Acms.Bll.Page item = enumeratedItem as Acms.Bll.Page;
            return new PageHierarchyData(item);
        }

        #endregion

        #region IEnumerable Members

        public System.Collections.IEnumerator GetEnumerator()
        {
            return _pages.GetEnumerator();
        }

        #endregion
    }

    public class PageHierarchyData : IHierarchyData
    {
        public PageHierarchyData(Acms.Bll.Page page)
        {
            _page = page;
        }
        private Acms.Bll.Page _page;

        #region IHierarchyData Members

        public IHierarchicalEnumerable GetChildren()
        {
            return new PageHirechicalEnumerable(_page.Pages); 
        }

        public IHierarchyData GetParent()
        {
            return new PageHierarchyData(_page.Collection.Parent);
        }

        public bool HasChildren
        {
            get 
            {
                if (_page.Pages == null || _page.Pages.Count == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        public object Item
        {
            get { return _page; }
        }

        public string Path
        {
            get 
            {
                StringBuilder tmp = new StringBuilder();
                Acms.Bll.Page currrent = _page;
                string spacer = "";
                while (currrent != null)
                {
                    tmp.Insert(0, spacer);
                    tmp.Insert(0, currrent.Name);
                    if (currrent.Collection != null && currrent.Collection.Parent != null)
                    {
                        currrent = currrent.Collection.Parent;
                    }
                    else
                    {
                        currrent = null;
                    }
                    spacer = "/";
                }
                return tmp.ToString();
            }
        }

        public string Type
        {
            get { return _page.GetType().ToString(); }
        }

        #endregion
    }
}
